package com.latvia.utils;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;

import org.json.JSONArray;

import android.app.ProgressDialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;

import com.latvia.cookingapp.R;
import com.latvia.model.Recipe;

public class JsonQueryAsyncTask extends
		AsyncTask<Void, Void, ArrayList<Recipe>> {
	public String searchResult = "";
	public static final String JSON_URL = "imagine_recipe_server.json";

	private OnTaskCompletedListener listener;
	private ProgressDialog progressDialog;
	private Context context;
	private String query;
	private int parseMode;

	public interface OnTaskCompletedListener {
		void onQueryTaskCompleted(ArrayList<Recipe> result);
	}

	public void setOnTaskCompletedListener(OnTaskCompletedListener listener) {
		this.listener = listener;
	}

	public JsonQueryAsyncTask(Context con, String query, int parseMode) {
		this.context = con;
		this.query = query;
		this.parseMode = parseMode;
	}

	@Override
	protected ArrayList<Recipe> doInBackground(Void... params) {
		try {
			return getJsonQuery(query);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	@Override
	protected void onPreExecute() {
		super.onPreExecute();
		progressDialog = new ProgressDialog(context);
		 progressDialog.setMessage(context.getResources().getString(R.string.loading));
		progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
		progressDialog.setIndeterminate(true);
		progressDialog.setCancelable(false);
		progressDialog.show();
	}

	@Override
	protected void onPostExecute(ArrayList<Recipe> result) {
		super.onPostExecute(result);
		if (listener != null)
			listener.onQueryTaskCompleted(result);
		this.progressDialog.dismiss();
	}

	private ArrayList<Recipe> getJsonQuery(String query) throws IOException {
		try {
			URL requestURL = new URL(query);
			HttpURLConnection conn = (HttpURLConnection) requestURL
					.openConnection();
			conn.setRequestMethod("GET");

			
			

			if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
				
			}
			conn.connect();
			
			InputStream is = conn.getInputStream();

			// Read the stream
			byte[] b = new byte[1024];
			ByteArrayOutputStream baos = new ByteArrayOutputStream();

			while (is.read(b) != -1)
				baos.write(b);

			String JSONResp = new String(baos.toByteArray());

			JSONArray arr = new JSONArray(JSONResp);
			
			
			if (parseMode == Constants.recipes) {
				
			}
			ArrayList<Recipe> recipes = JsonParser.parseAllRecipes(arr);

			return recipes;
		} catch (Exception e) {

			e.printStackTrace();
			return null;
		}
	}

	// Image caching - need to add internal storage path later
	private Bitmap downloadImage(String surl, Recipe recipe) {
		Bitmap bitmap = null;
		try {
			URL url = new URL(surl);

			HttpURLConnection httpConn = (HttpURLConnection) url
					.openConnection();
			httpConn.setRequestMethod("GET");
			httpConn.connect();
			
			

			InputStream input = null;
			if (httpConn.getResponseCode() == HttpURLConnection.HTTP_OK) {
				input = httpConn.getInputStream();

				bitmap = BitmapFactory.decodeStream(input);
				String localPath = "image_path";

				ByteArrayOutputStream bytes = new ByteArrayOutputStream();
				bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bytes);

				File file = new File(localPath);
				file.createNewFile();

				FileOutputStream output = new FileOutputStream(file);
				output.write(bytes.toByteArray());
				output.flush();
				output.close();

				input.close();
				httpConn.disconnect();
			}
		} catch (Exception e) {
			e.printStackTrace();

		}

		return bitmap;
	}

}
